package com.woniu.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.ProPurchase;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 *<p>
 *采购的Mapper接口
 *</p>
 * @author ZhouWu
 * @date 2022/8/30 15:37
 */
public interface ProPurchaseMapper extends BaseMapper<ProPurchase> {

    //增加采购
    @Insert("insert into pro_purchase(pro_id,pur_name,pur_detail,supplier,pur_price,pur_num,pur_date,pur_status) values " +
            "(#{project.proId},#{purName}, #{purDetail},#{supplier},#{purPrice},#{purNum},#{purDate},#{purStatus}) ")
    void add(ProPurchase proPurchase);

    //删除采购
    @Delete("delete from pro_purchase where pur_id=#{purId}")
    void delete(int purId);

    //动态更新采购的列表
    @UpdateProvider(type = SqlProvider.class,method = "update")
    void update(ProPurchase proPurchase );

    //根据采购id查单个采购
    @Select("select pur.*, pro.pro_name from pro_purchase pur, project pro  where pur.pur_id=#{purId} and pur.pro_id = pro.pro_id ")
    @Results(id="proPurchaseMap" ,value = {
            @Result(column = "pur_id", property = "purId",id = true),
            @Result(column = "pro_id", property = "project.proId"),
            @Result(column = "pro_name", property = "project.proName"),
            @Result(column = "pur_name", property = "purName"),
            @Result(column = "pur_detail", property = "purDetail"),
            @Result(column = "supplier", property = "supplier"),
            @Result(column = "pur_price", property = "purPrice"),
            @Result(column = "pur_num", property = "purNum"),
            @Result(column = "pur_date", property = "purDate"),
            @Result(column = "pur_status", property = "purStatus")
    })
    ProPurchase getByPurId(int purId);

    //查询状态为Y的所有的采购，状态为Y，也就是正在进行中的采购
    @Select("select pur.*, pro.pro_name from pro_purchase pur, project pro  where pur.pro_id = pro.pro_id and pur_status='y' ")
    @ResultMap("proPurchaseMap")
    List<ProPurchase> getAll();

    //根据项目的id查询采购的信息
    @Select("select pur.*, pro.pro_name from pro_purchase pur, project pro  where pur.pro_id=#{project.id} and pur.pro_id = pro.pro_id ")
    @ResultMap("proPurchaseMap")
    List<ProPurchase> getByProId(int proId);

    //多条件分页查询采购并分页
    @Select("select * from pro_purchase ${ew.customSqlSegment} and pur_status='y' ")
    Page<ProPurchase> getProPurchaseByPage(Page<ProPurchase> page,@Param(Constants.WRAPPER) Wrapper<ProPurchase> wrapper);

    //多条件查询采购
    @Select("select * from pro_purchase ${ew.customSqlSegment} and pur_status='y' ")
    @Results(id="proPurchaseExcelMap" ,value = {
            @Result(column = "pur_id", property = "purId",id = true),
            @Result(column = "pro_id", property = "project.proId"),
            @Result(column = "pur_name", property = "purName"),
            @Result(column = "pur_detail", property = "purDetail"),
            @Result(column = "supplier", property = "supplier"),
            @Result(column = "pur_price", property = "purPrice"),
            @Result(column = "pur_num", property = "purNum"),
            @Result(column = "pur_date", property = "purDate"),
            @Result(column = "pur_status", property = "purStatus")
    })
    List<ProPurchase> getProPurchaseByCondition (@Param(Constants.WRAPPER) Wrapper<ProPurchase> wrapper );

    public static class SqlProvider {
        public static String update(ProPurchase proPurchase) {
            String sql = new SQL(){
                {
                    //条件写法
                    UPDATE("pro_purchase");
                    if(proPurchase.getProject().getProId() != 0 && proPurchase.getProject() != null ) {
                        SET("pro_id=#{project.proId}");
                    }
                    if(proPurchase.getPurName() != null ) {
                        SET("pur_name=#{purName}");
                    }
                    if(proPurchase.getPurDetail() != null ) {
                        SET("pur_detail=#{purDetail}");
                    }
                    if(proPurchase.getSupplier() != null ) {
                        SET("supplier=#{supplier}");
                    }
                    if(proPurchase.getPurPrice() != null ) {
                        SET("pur_price=#{purPrice}");
                    }
                    if(proPurchase.getPurNum() != 0 ) {
                        SET("pur_num=#{purNum}");
                    }
                    if(proPurchase.getPurDate() != null ) {
                        SET("pur_date=#{purDate}");
                    }
                    if(proPurchase.getPurStatus() != null ) {
                        SET("pur_status=#{purStatus}");
                    }
                    WHERE("pur_id=#{purId}");
                }
            }.toString();
            return sql;
        }
    }

}
